package com.sxhalo.manage.web.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sxhalo.manage.map.SysUsers;
import com.sxhalo.manage.map.Task;
import com.sxhalo.manage.map.TaskLog;
import com.sxhalo.manage.service.IBaseService;
import com.sxhalo.manage.utils.Pager;
import com.sxhalo.manage.web.form.TaskLogForm;

public class TaskLogAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private int draw = 1;

	private int start = 0;

	private int length = 20;

	private TaskLog tasklog;

	// 将会被Struts2序列化为JSON字符串的对象
	private Map<String, Object> dataMap;
    
    private List<TaskLogForm> dataList;
	// 业务层
	private IBaseService<TaskLog> myService;
	
	private IBaseService<Task> taskService;
	
	public int getDraw() {
		return draw;
	}

	public void setDraw(int draw) {
		this.draw = draw;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public TaskLog getTasklog() {
		return tasklog;
	}

	public void setTasklog(TaskLog tasklog) {
		this.tasklog = tasklog;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public List<TaskLogForm> getDataList() {
		return dataList;
	}

	public void setDataList(List<TaskLogForm> dataList) {
		this.dataList = dataList;
	}

	public IBaseService<TaskLog> getMyService() {
		return myService;
	}

	public void setMyService(IBaseService<TaskLog> myService) {
		this.myService = myService;
	}

	public IBaseService<Task> getTaskService() {
		return taskService;
	}

	public void setTaskService(IBaseService<Task> taskService) {
		this.taskService = taskService;
	}

	/**
	 * 构造方法
	 */
	public TaskLogAction() {
		// 初始化Map对象
		dataMap = new HashMap<String, Object>();
	}

    public String getEntity() {
		try {
			Task task = taskService.findById(tasklog.getTaskId());
			tasklog.setTaskLogPercent(task.getTaskPercentComplete());
			tasklog = myService.findById(tasklog.getTaskLogId());
		} catch (Exception re) {
			
		}
		// 返回结果
		return SUCCESS;
	}

    public String getGridData() {
		// dataMap中的数据将会被Struts2转换成JSON字符串，所以这里要先清空其中的数据
		dataMap.clear();
		// 获取数据
		try {
			String whereStr = "";
            String tid = ServletActionContext.getRequest().getParameter("tid");
            if(tid!=null && !tid.equals("")){
            	whereStr += " taskId = "+ tid;
            }	
			int page = start == 0 ? 1 : start / length + 1;
			int size = length < 0 ? Integer.MAX_VALUE : length;
			Pager<TaskLog> list = myService.findPagerList(page, size, whereStr, "");
			dataMap.put("draw", draw);
			dataMap.put("recordsTotal", list.getTotal());
			dataMap.put("recordsFiltered", list.getTotal());
			dataMap.put("data", list.getList());
		} catch (Exception re) {
			dataMap.put("data", re.getMessage());
		}
		// 返回结果
		return SUCCESS;
	}
  
    public String getAllData() {
		// 获取数据
        try {
        	List<TaskLogForm> data = new ArrayList<TaskLogForm>();
        	List<TaskLog> list = myService.findAll();
        	for(TaskLog po:list){
        		TaskLogForm vo = new TaskLogForm();
        		BeanUtils.copyProperties(po, vo);
        		data.add(vo);
        	}
        	dataList = data;
        } catch (Exception re) {
			
		}
		// 返回结果
		return SUCCESS;
	}
    public String addEntity() {
		try {
			Timestamp startDate = new Timestamp(System.currentTimeMillis());
			SysUsers user = (SysUsers)ActionContext.getContext().getSession().get("user");
			TaskLog entity = new TaskLog();
			BeanUtils.copyProperties(tasklog, entity);
			entity.setTaskLogDate(startDate);
			if(user!=null){
				entity.setTaskLogCreator(user);
			}
			Task task = taskService.findById(tasklog.getTaskId());
			if(task!=null){
				task.setTaskPercentComplete(tasklog.getTaskLogPercent());
				taskService.update(task);
			}
			myService.save(entity);
		} catch (Exception re) {

		}
		return SUCCESS;
	}

	public String editEntity() {
		try {
			TaskLog entity = myService.findById(tasklog.getTaskLogId());
			entity.setTaskLogName(tasklog.getTaskLogName());
			entity.setTaskLogDescription(tasklog.getTaskLogDescription());
			entity.setTaskLogType(tasklog.getTaskLogType());
			entity.setTaskLogPercent(tasklog.getTaskLogPercent());
			Task task = taskService.findById(tasklog.getTaskId());
			if(task!=null){
				task.setTaskPercentComplete(tasklog.getTaskLogPercent());
				taskService.update(task);
			}
			myService.update(entity);
		} catch (Exception re) {
			
		}
		return SUCCESS;
	}

	public String delEntity() {
		// 清空dataMap中的数据
        dataMap.clear();
		try {
			TaskLog entity = myService.findById(tasklog.getTaskLogId());
				if(myService.delete(entity)){
					dataMap.put("valid", true);
				} else {
					dataMap.put("valid", false);
				}
			} catch (Exception re) {
				dataMap.put("valid", false);
			}
		// 返回结果
		return SUCCESS;
	}
}
